<template>
  <div class='u-applyPayDialog'>
    <el-dialog ref='dialog'
      title="提交收款申请"
      :visible.sync="dialogVisible"
      :before-close="() => {this.dialogVisible = false}"
    >
      <div class="title">{{ selectOrders.length || 0 }}笔款项共计{{ price || 0 }}元</div>
      <el-form :inline='true' ref='form' :model="form" :rules='rules' label-width="100px">
        <el-form-item label='收款方式' prop='type'>
          <a-select v-model='form.type' :options='options' />
        </el-form-item>
        <el-form-item label='备注'>
          <el-input type='textarea' :rows='4' v-model.trim='form.remark' />
        </el-form-item>
        <div class='btnWrap'>
          <el-button type='primary' @click='confirm'>确定</el-button>
          <el-button @click='dialogVisible = false'>取消</el-button>
        </div>
      </el-form>
    </el-dialog>
  </div>
</template>

<script>
export default {
  name: 'u-applyPayDialog',

  props: ['selectOrders'],

  data() {
    return {
      dialogVisible: false,
      ifRequsting: false,
      form: {
        type: '',
        remark: ''
      },
      rules: {
        type: [
          { required: true, message: '请选择收款方式', trigger: 'blur' }
        ]
      },
      options: [
        { label: '微信', value: '1' },
        { label: '支付宝', value: '2' },
        { label: '公司公账', value: '4' },
        { label: '公司私账', value: '5' }
      ]
    }
  },

  computed: {
    price() {
      let price = 0
      this.selectOrders.forEach(v => {
        price += v.payment
      })
      return price
    }
  },

  methods: {
    show() {
      this.dialogVisible = true
      if (this.$refs.form) {
        this.clear()
      }
    },

    clear() {
      this.$refs.form.resetFields()
    },

    async confirm() {
      if (this.ifRequsting) return false
      this.$refs.form.validate(async (valid) => {
        if (!valid) return
        this.ifRequsting = true
        const { code } = await this.$api.orderSellerPay({
          aMerchantId: this.aFirmId,
          aOrderIds: this.selectOrders.map(v => v.order_id),
          aPayType: this.form.type,
          memo: this.form.remark
        })
        this.ifRequsting = false
        if (code === 0) {
          this.dialogVisible = false
          this.$emit('update')
          this.__notify('提示', '操作成功')
        }
      })
    }
  }
}
</script>

<style lang='less'>
.u-applyPayDialog {
  .title {
    font-size: 16px;
    text-align: center;
    padding-bottom: 20px;
  }
  .btnWrap {
    display: flex;
    justify-content: center;
    padding-top: 20px;
  }
  .el-form-item  {
    width: 100%;
    display: flex;
    align-items: center;
  }
  .el-form-item__content {
    width: 100%;
  }
}
</style>